package com.andware.volley.toolbox;

import android.util.Log;

import com.andware.volley.NetworkResponse;
import com.andware.volley.Response;
import com.andware.volley.VolleyError;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;

/**
 * Created by ishishuji on 14-7-10.
 */
@SuppressWarnings ("ALL")
public class SubNoGsonStringRequest < T > extends JsonRequest< String > {

    public static final Gson mGson = new Gson ();

    public SubNoGsonStringRequest ( String url, Response.Listener< String > listener, Response.ErrorListener errorListener ) {
        super ( url, null, listener, errorListener );
    }

    public SubNoGsonStringRequest ( int method, String url, Response.Listener< String > listener, Response.ErrorListener errorListener ) {
        super ( method, url, null, listener, errorListener );
    }

    public SubNoGsonStringRequest ( int method, String url, String requestBody, Response.Listener< String > listener, Response.ErrorListener errorListener ) {
        super ( method, url, requestBody, listener, errorListener );
        Log.i ( "PostValue", "value:" + mGson.toJson ( requestBody ) );
    }


    @Override
    protected Response< String > parseNetworkResponse ( NetworkResponse response ) {
        String parsed;
        try {
            parsed = new String ( response.data, HttpHeaderParser.parseCharset ( response.headers ) );
        } catch ( UnsupportedEncodingException e ) {
            parsed = new String ( response.data );
        }
        return Response.success ( parsed, HttpHeaderParser.parseCacheHeaders ( response ) );
    }

    @Override
    protected VolleyError parseNetworkError ( VolleyError volleyError ) {
        if ( volleyError.networkResponse != null && volleyError.networkResponse.data != null ) {
            VolleyError error = new VolleyError ( new String ( volleyError.networkResponse.data ) );
            volleyError = error;
        }

        return volleyError;
    }
}
